package com.gh.cloud.commons.core.model;

import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;

/**
 * @author: guohao
 * @date: 2021-02-07 17:46
 **/
@Data
public class LoginUser implements UserDetails {

    private static final long serialVersionUID = 572798295759947733L;
    private String userId;

    private String userName;

    private String nickName;

    private String trueName;

    private String password;

    private List<String> ruleList;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        Collection<GrantedAuthority> collection = new HashSet<>();
        if (!CollectionUtils.isEmpty(ruleList)) {
            this.ruleList.forEach(role -> collection.add(new SimpleGrantedAuthority(role)));
        }
        return collection;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return userName;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
